<?php
/**
 * HTTPRunner run HTTP request.
 * @package Core
 * @subpackage Runner
 * @category Runner
 * @author kovacsricsi
 */

class HTTPRunner implements IRunner
{
	/**
	 * Controller to run.
	 * @access protected
	 * @var array
	 */
	protected $_controller;

    /**
     * Session handler.
     * @access protected
     * @var SessionHandler
     */
    protected $_sessionHandler;

	/**
	 * Contructor.
	 * @access public
	 * @return void
	 */
	public function __construct()
	{
		/**
		 * start sesion
		 */
        $this->_sessionHandler = new SessionHandler();

		$this->_controller = Router::getController();
	}

	/**
	 * Run function - Loads the corresponnding controller and set X-System header.
	 * @access public
	 * @return void
	 */
	public function run()
	{        
		header("X-System-Design-1:kRichard Kovacs");
		header("X-System-Design-1-Contact: kovacsricsi@gmail.com");
		header("X-System-Design-1-Home: http://mhmxs.mine.nu");

		try {
			$parameter = isset($this->_controller["parameter"]) ? $this->_controller["parameter"] : null;
			$function  = isset($this->_controller["function"]) ? $this->_controller["function"] : null;

			$controller = new $this->_controller["controller"]($parameter, $function);
		} catch (Exception $e) {
			HTTP::sendHeader("HTTP/1.1 503 Server Unavailable", true, 503);
			HTTP::write("<h1>503 error!</h1>");
		}
	}

	/**
	 * Destructor
	 * @access public
	 * @return void
	 */
	public function __destruct()
	{
		$this->_sessionHandler->writeSession();
	}
}
?>